﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace DIProject.Util
{
    public class Setting2
    {
        public static void Save(Util.SettingFile setting)
        {
            XmlSerializer ser = new XmlSerializer(typeof(SettingFile));
            if(System.IO.File.Exists(Setting2.Path))
                System.IO.File.Delete(Setting2.Path);
            using (FileStream fs = new FileStream(Setting2.Path, FileMode.Create, FileAccess.Write))
            {
                using (TextWriter writer = new StreamWriter(fs))
                {
                    ser.Serialize(writer, setting);
                }
            }
        }
        public static Util.SettingFile GetSetting()
        {
            if (!System.IO.File.Exists(Setting2.Path))
            {
                Util.SettingFile res = new SettingFile();
                return res;
            }
            XmlSerializer ser = new XmlSerializer(typeof(SettingFile));
            using (FileStream fs = new FileStream(Setting2.Path, FileMode.Open))
            {
                using (TextReader tr = new StreamReader(fs))
                {
                    Util.SettingFile res = (Util.SettingFile)ser.Deserialize(tr);
                    return res;
                }
            }
        }
        public static string Path
        {
            get
            {
                string str = Directory.GetCurrentDirectory() + "/Setting.xml";
                return str;
            }
        }
    }
}
